import os
import shutil
from tqdm import tqdm
import trimesh
import csv


def extract_core(src_path, dst_path):
    os.makedirs(dst_path, exist_ok=True)
    categories = {"bottle": "02876657",
                  "bowl": "02880940",
                  "camera": "02942699",
                  "can": "02946921",
                  # "car": "02958343",
                  "cellphone": "02992529",
                  "jar": "03593526",
                  "knife": "03624134",
                  "mug": "03797390",
                  "pillow": "03938244",
                  "pistol": "03948459",
                  "remote": "04074963"}
    bad_instances = [
        "bottle-5872e807edaf985878fe949fc1419876",
        "bottle-c3bc0cd2648e76b9187964c89b3399f1",
        "bottle-e90e0506fd00fe93f42d6bd378df1c70",
        "bowl-1a0a2715462499fbf9029695a3277412",
        "bowl-4cf18594e8851a3d3a5e6305a3a7adee",
        "bowl-446583d7bf857dced5cb6d178687b980",
        "bowl-18529eba21e4be8b5cc4957a8e7226be",
        "bowl-da5f13f4048dbd72fcb8d8c6d4df8143",
        "camera-3b56239e45828d2bb4521a835b1946c8",
        "camera-7ef29f8a7a132c46e0afa7d1aded497",
        "camera-9cdaf68ed1e1daba9c21adf7cd249be9",
        "camera-9db4b2c19c858a36eb34db531a289b8e",
        "camera-89f0e0da4747aad1172ac9cfeff21994",
        "camera-68f66f5ab594fd3cc2890daf3a9f7413",
        "camera-416ce9adf7ad40f4959c1c3d740c4f1",
        "camera-1298634053ad50d36d07c55cf995503e",
        "camera-ce40b134b11e8c822bbc2c380e91dfe2",
        "camera-d9bb9c5a48c3afbfb84553e864d84802",
        "camera-d1a3482c576f8c50592ecd319dfd8c5d",
        "camera-c802792f388650428341191174307890",
        "camera-ff74c4d2e710df3401a67448bf8fe08",
        "cellphone-1e597e56b6b2fef6c331b59dc8938ad",
        "cellphone-8b17963baf2e54f87a0b781a26650bed",
        "cellphone-40dbb04ab8f780d6cc571cd3cf8f17a1",
        "cellphone-1101db09207b39c244f01fc4278d10c1",
        "cellphone-14726c2482a092d3a9fe1734a6086750",
        "cellphone-73339a7ed1e72d89aef10502d58d4967",
        "cellphone-f48cb1a242e99745ab905891b6fa1a",
        "jar-1a953dddff37998ef0a5dc57c9d2438f",
        "jar-1c47c52b9f57f348e44d45504b29e834",
        "jar-1a01c2d7705193b4917cb569753af492",
        "jar-2d5cccc84167c652b5918a323a1320cb",
        "jar-1fc8104630b00416c151419181ef256",
        "jar-2d69fad249c53ca0c6e02b40b50fb3e7",
        "jar-3a6e1c3f0755d58d774ba10f0bcc178a",
        "jar-3b5397782a5a1145fdbe2bf897d8a820",
        "jar-3c70ecd8d8c92f7b2ff65fa033da81db",
        "jar-3c148476c78123f3283b00891f680579",
        "jar-3cd46f573c68e5fcfd41ffe96f6ef4b4",
        "jar-3ce3a0a7adbaaa518e43a810a63361f0",
        "jar-3e79d011230224283307b6a54feea334",
        "jar-4d054c0a0c2fd8e4503adffe35460705",
        "jar-4ed93b27b2b1ae4a17d01689ded74b74",
        "jar-5d3c251a9c593929c268c38e2d88a8f5",
        "jar-5c589d9495c87d7da2fcca81c767066f",
        "jar-5e61ce9dd25efc97eb7b988bf4f0d1ef",
        "jar-6bc426e7226f6f22362d7fcfd32ff15d",
        "jar-6bce9e16470b57d0c5beea20858a99d5",
        "jar-6c47d6e5ee3f6a5da8f6851a5d3f13b6",
        "jar-6dfb59f33c88a4e0263c42ff6d6b9672",
        "jar-7cd890d57c3d5705a0c6f54ff9ff2ca5",
        "jar-8da5db60ba2c5f9318694f1cc6fb1f8",
        "jar-8c8f6d2b7b2d8dfb60cbb0bb881f838f",
        "jar-9a5cb53a2858225e72a5c49c981e6593",
        "jar-9aff149b3b73200fb4b94732d6aa086d",
        "jar-9d1a07991391ecec6b7154919b02cbec",
        "jar-10af6bdfd126209faaf0ad030fc37d94",
        "jar-20de70652a72eb4ae27790b0ec8671f7",
        "jar-24d09682cc8ead67191c3762b497eca9",
        "jar-30b7bc199de66fc57eaeab1f0c9120b7",
        "jar-41dc952ae902bb2d2a087337e55e3e2e",
        "jar-44a5d28686a9b8c9182816640e3b1915",
        "jar-45ba72a5187c5fd7b9c6deef486a7d8",
        "jar-48c94c6283fa5566a4136492f17b9a59",
        "jar-48eaa8465573a112bba080e9e0b1ed91",
        "jar-67f7d931a1796b97331dae093cef31",
        "jar-72b25fa022376928e4feabc80d16b5e4",
        "jar-78c709102ee8d89ab7d0f3d89bbac230",
        "jar-84a8a8a36901d3c4500158c23c4c5a8e",
        "jar-92fa811a0cc76fbefffe8e781bf3bc2",
        "jar-507b672473eda11032a1eca0e8e4f468",
        "jar-472eba74eb73dcb9c5beea20858a99d5",
        "jar-585c1079643d859db2236bb2a62ecfdf",
        "jar-712db68c0889d82925e0f641bff885da",
        "jar-912c3e1cf2ef3b9ea7d0cc9b15400f65",
        "jar-2293b6aa5ed35db3b1a98fe8994d06b6",
        "jar-915ca876e784d965c5b724995774ac86",
        "jar-2872c4b8e2f86201b7d0f3d89bbac230",
        "jar-3304e9400b2d610a69eaff4d4a8a103",
        "jar-7797bf6a6f3f4a809e9e01b160c24e12",
        "jar-4784aeb9d867aa83f685327911e7e258",
        "jar-7037b6266188af53197a35146ee825cd",
        "jar-9215bfacbcac3cb67697d9c3588bbfc5",
        "jar-28938abdb2114e53bbc7a6acbd8f058b",
        "jar-70367e4fd1ad753427eb00c151c6f711",
        "jar-76677da63039c6d9e6a735b744b69aa4",
        "jar-6628119d7cf6ca8b82db9fca4b68095",
        "jar-44878567a2cae5f8b5e1005644816d9f",
        "jar-63447253a06727004725f67267e31c89",
        "jar-82298657eaa980d323a295070ca176a0",
        "jar-41391118c374e765ff3ef8f363b2cc43",
        "jar-9195960e68825d41156d1d06c447a736",
        "jar-330880146fd858e2cb6782d0f0acad78",
        "jar-3879379848c2c0c55b2f23b9c7b146a8",
        "jar-489317571832ea4c15d3518b43b3bc22",
        "jar-a99b8679b072b0cf9b6e5e087210d348",
        "jar-a263bb1f29feb8354dc11e421397c795",
        "jar-a791d4fa1d840408c5beea20858a99d5",
        "jar-ac3fa82107f01502cba92cca5bb025de",
        "jar-ae81dc3d42c59e8c500158c23c4c5a8e",
        "jar-af4c5fab6584a6ead5cc6270d226b60d",
        "jar-afeb60dda62138a04013b7b0fb1ceaff",
        "jar-b2e502425dc3f07bfe4f69210ea5cdca",
        "jar-b8e8182813626e7a3792407aee21900f",
        "jar-b517eed6f77a7a39586574d8cfbafba0",
        "jar-bacb6b671e5904d8274d4393e80e99fa",
        "jar-badbcf9d64bd24c868c6661a48aa297e",
        "jar-bcf95c3997bf686b4beac2065c0d9cb7",
        "jar-c0d6cd0537d0fcc8235e3c816d887b3",
        "jar-be4e86198d0b8d438edb5be732846826",
        "jar-c7bb8b9685f0378307f509cdf07ef62",
        "jar-c795ed7d374e893964558a1c6a2a9f11",
        "jar-cd03a7bd6ace5432e7a3b2beb7306f11",
        "jar-cf184ee375cf827eeee0d5e82f0b9cab",
        "jar-d2b8e090125199c49edbf2f02d126923",
        "jar-d5ffa68a7be137ab157d60b585b8fb6e",
        "jar-d68da52ed2de0e7bc5beea20858a99d5",
        "jar-dd815275fce18cdfd138a53ab0d038a5",
        "jar-d214b7c1d3b40f935e7cfd06717825f5",
        "jar-d214b7c1d3b40f935e7cfd06717825f5",
        "jar-dd815275fce18cdfd138a53ab0d038a5",
        "jar-de948c52437de4d2a57b3bec78e5d1b3",
        "jar-dece4e1eb0d2f202150a45ec52bcbd7",
        "jar-de948c52437de4d2a57b3bec78e5d1b3",
        "jar-dfebdbe996e582f453b5e86c4b574a4b",
        "jar-e8a0fca9bfb4422cff3ef8f363b2cc43",
        "jar-e70b94a9058952643f88f523fa8b6921",
        "jar-e142e0414bbe9b39cb6782d0f0acad78",
        "jar-e74cb96c0fc30d03993afecc2c675915",
        "jar-e0855a7115eda487186588b1d2fb9823",
        "jar-ee07000195ef7c93e89c405d853359af",
        "jar-f232cafa6b5d570ac5beea20858a99d5",
        "jar-f84313fa79e1bbd3d826e39f6130227f",
        "jar-f9346339a3b24bfc500158c23c4c5a8e",
        "jar-fbaee96700018ab3835b141fda2da5ed",
        "jar-f8ba5c5c9530a2d43dddf957f22bddb2",
        "jar-f8e8740b44ca276abb6f5a9e4b6b8b34",
        "jar-f9ae4485b706ab1893fa5ed2239f9b08",
        "jar-ff24afcf157e1b95bc311577a374c00c",
        "knife-2c23a56cbc058d5356c5c8316540f708",
        "knife-ceb3b39c9a035752b4fc059d1d10ec5d",
        "mug-5ef0c4f8c0884a24762241154bf230ce",
        "mug-6c04c2eac973936523c841f9d5051936",
        "mug-6d2657c640e97c4dd4c0c1a5a5d9a6b8",
        "mug-b9be7cfe653740eb7633a2dd89cec754",
        "pillow-218f86362028a45b78f8b40f4a2ae98a",
        "pillow-ec4b9c23be44b35992cd8215a5562ce",
        "pistol-9b19164c5b17f3016da79efb34b8c0b4",
        "pistol-4af05095ad02dd8c9cd99d3d15a8756e",
        "pistol-434a15053bf9d88c670144fdfc186d25",
        "pistol-c34d4ec7a3340f375e9786b1603f45c8",
    ]

    bad_instances = set(bad_instances)

    cnt = 0

    for cat, code in categories.items():
        for mesh in tqdm(os.listdir(os.path.join(src_path, code))):
            if not os.path.exists(os.path.join(src_path, code,
                                               mesh, "models/model_normalized.obj")):
                continue
            if cat + "-" + mesh in bad_instances:
                continue
            cnt += 1
            # print(os.path.join(dst_path, "core-" + cat + "-" + mesh + ".obj"))
            shutil.copyfile(os.path.join(src_path, code,
                            mesh, "models/model_normalized.obj"), os.path.join(dst_path, "core-" + cat + "-" + mesh+".obj"))

    print("Extracted from ShapeNetCore:", cnt)


def extract_sem(src_path, dst_path, meta_path):
    os.makedirs(dst_path, exist_ok=True)
    bad_categories = [
        "1Shelves",
        "2Shelves",
        "3Shelves",
        "4Shelves",
        "5Shelves",
        "6Shelves",
        "7Shelves",
        "AccentChair",
        "AccentTable",
        "AirConditioner",
        "ArcadeMachine",
        "Armoire",
        "Bag",
        "Barstool",
        "BarTable",
        "Basket",
        "Backpack",
        "Bathtub",
        "Bed",
        "Bench",
        "Bicycle",
        "Bidet",
        "Blind",
        "Board",
        "Bookcase",
        "Books",
        "Booth",
        "Bucket",
        "Cabinet",
        "Cabling",
        "Cage",
        "CanopyBed",
        "CeilingFan",
        "CeilingLamp",
        "Chair",
        "ChairWithOttoman",
        "Chaise",
        "ChessBoard",
        "ChestOfDrawers",
        "ChildBed",
        "CoatRack",
        "CoffeeMaker",
        "CoffeeTable",
        "Computer",
        "Counter",
        "Credenza",
        "Copier",
        "Couch",
        "CurioCabinet",
        "Curtain",
        "CuttingBoard",
        "Cradle",
        "Crib",
        "Dart",
        "DartBoard",
        "DecorativeAccessory",
        "Desk",
        "DeskLamp",
        "Desktop",
        "DiningTable",
        "Dishwasher",
        "Door",
        "DoubleBed",
        "DraftingTable",
        "Dresser",
        "DresserWithMirror",
        "DrumSet",
        "Dryer",
        "Easel",
        "EndTable",
        "Fan",
        "Faucet",
        "FileCarbinet",
        "FilePlace",
        "Fireplace",
        "FlagPole",
        "FloorLamp",
        "GameTable",
        "Guitar",
        "GuitarStand",
        "HandDryer",
        "Hanger",
        "Headboard",
        "Helicopter",
        "IroningBoard",
        "Keyboard",
        "KingBed",
        "KneelingChair",
        "Lamp",
        "Laptop",
        "LDesk",
        "Lectern",
        "LightSwitch",
        "LoftBed",
        "Loveseat",
        "Magnet",
        "Mattress",
        "MediaPlayer",
        "MediaStorage",
        "Microwave",
        "Mirror",
        "Monitor",
        "MousePad",
        "Nightstand",
        "OfficeChair",
        "OfficeSideChair",
        "Ottoman",
        "OutdoorTable",
        "Oven",
        "OvenTop",
        "Painting",
        "Paper",
        "PaperBox",
        "PaperClip",
        "PaperMoney",
        "Pedestal",
        "PianoKeyboard",
        "Picture",
        "PictureFrame",
        "PicnivTableSet",
        "Plant",
        "Plotter",
        "Poster",
        "PosterBed",
        "PotRack",
        "PottedPlant",
        "PowerSocket",
        "Printer",
        "Purse",
        "QueenBed",
        "QueenBedWithNightstand",
        "Rack",
        "Recliner",
        "Refrigerator",
        "Ring",
        "Room",
        "RoundBed",
        "RoundTable",
        "Rug",
        "Sectional",
        "Shirt",
        "ShoppingCart",
        "Shoes",
        "Shower",
        "Sideboard",
        "SideChair",
        "SingleBed",
        "Sink",
        "Speaker",
        "StandingClock",
        "StaplerWithStaples",
        "Stool",
        "Suitcase",
        "Table",
        "TableLamp",
        "TelescopeWithTripod",
        "Tent",
        "Toilet",
        "TrashBin",
        "Trumpet",
        "TV",
        "TvStand",
        "Umbrella",
        "Vanity",
        "Violin",
        "WallArt",
        "WallArtWithFigurine",
        "WallDecoration",
        "WallLamp",
        "WallUnit",
        "Wardrobe",
        "Washer",
        "WasherDryerSet",
        "WebCam",
        "Whiteboard",
        "Window",
        "WineRack",
        "Wood",
    ]

    bad_instances = [
        "BeanBag-7814872586698c7f18b14db3b83de9ff",
        "Book-e89154bcf3d1311cef1b7984700142ff",
        "Book-4698973d47c780654f48c7d5931203ac",
        "Calculator-79bf4603c04aa8ae1cdded8ccd3a3fa1",
        "Camera-7ef29f8a7a132c46e0afa7d1aded497",
        "Camera-32f4aa21bdb87e5997a5d09d1e29f30b",
        "Camera-550aea46c75351a387cfe978d99ba05d",
        "Camera-77fcc441a09908f7257cfa25fc15f93f",
        "Camera-4570f175711c193cc01557b97c8aeedb",
        "Camera-879727590f0649c167964ba700cd97f5",
        "Camera-879727590f0649c167964ba700cd97f5",
        "Camera-a36da8db1e74034e5b62a49c19c16258",
        "Camera-b461b7bc9b9b8f523d90bc70e4e094c0",
        "Camera-c8b96929f167c1c01ddcf95bc305d1b0",
        "Camera-c802792f388650428341191174307890",
        "Camera-f1267c8d8cbaf4765a6ec2361631ee83",
        "Candle-4a95e7f0343134dacfe5d7e8cb5499d2",
        "Candle-18c499f4f1a833dee3f7a74e12a274ef",
        "Candle-5c792b8f5c0416adfef16454b9792325",
        "Candle-59f3bcb326cfb4878870cefd9ff03619",
        "Candle-26b72a9e18203a02f841233486fac2b",
        "Candle-1c5e5f1485ba5db1f879801ae14fa622",
        "Candle-2fc61d27861f88aa4c066935f7480f9e",
        "Candle-4d26cd5133e33f562846fa729d90e125",
        "Candle-450ef6f993de351ef3a54fdb52b446ad",
        "Candle-1061c1b4af7fd99777f4e9e0a7e4c2",
        "Candle-91048e8530f750d4409421506a05b6e1",
        "Candle-5526503b9089aa12b21e97993de5df16",
        "Candle-b6a099bbad33baeed49957356fca3ba9",
        "Candle-b4976b2fba66769d4cccbece4754c7cf",
        "Candle-bf7150430f5b5aa12f841233486fac2b",
        "Candle-e2424f593f696cab6774f953182a5253",
        "Candle-ee53c928afaa339253250612ca93852",
        "Candle-fdd0bb5981863b998c9441777325d0fd",
        "Cap-eca887a664e8965026da26adf4c8df7",
        "Cassette-d682ba2b7f1c01299c3cb90632bf17f3",
        "CellPhone-36ed8772c18897d7c58906a1817b2a41",
        "Clock-6cd9f0f548979d5160ddc468fe733ed1",
        "Clock-46320beb087955da408e862e5daf1757",
        "Clock-bbd8ce2c1eea06282f841233486fac2b",
        "ComputerMouse-47fc4a2417ca259f894dbff6c6a6be89",
        "ComputerMouse-379e93edfd0cb9e4cc034c03c3eb69d",
        "ComputerMouse-3959f60f4e4356b35817e30de1dabac4",
        "ComputerMouse-6560e938bffb3df1d0063b5c282d168a",
        "ComputerMouse-898326912ab0c124b04e3108b1fce4da",
        "ComputerMouse-a6d442101c252b09e131a48033aecfb6",
        "ComputerMouse-ce7719a96f3f9cdc6bdcd672c2b17215",
        "ComputerMouse-f1a73db7cf61a17a57cdf426cf592f9a",
        "ComputerMouse-f5c685fad75ec9132f81cb887dc35578",
        "Dog-63cac13e3f855bc97044daeae7ed1900",
        "Dog-8691c6558e4d22d15f4cdfe420b20d8e",
        "DrinkingUtensil-d1449f1d3d442543ca742aeb2f07ef4c",
        "FoodItem-3ffae4454d205ac8bf23a820d8bbc1f8",
        "FoodItem-78bd798a70addbad9c3cb90632bf17f3",
        "FoodItem-38fe68125fb895ba5898f9335ccc83b",
        "FoodItem-546c06e9e16eac44dd37f1676f84f13",
        "FoodItem-68a99bcc0284ef67356f94fde5becb81",
        "FoodItem-33428fb1b4dc220aeaa19e0f97dcbe0",
        "FoodItem-bc64294a74e509b6e43969badba53c7b",
        "Gun-6d3bfd984d7f3105b96aa5dc23c036c",
        "Headphones-24aea80f4922897f667d013d9903dad4",
        "Headphones-90b4111dd6d3d7c82f664f563dc2f43d",
        "Headphones-526af25f6ca59fbf8c87b15380286afc",
        "Ipod-3c85687ed8b3f4844e9b2d8ebb2f0b0",
        "Ipod-4f3f82dc3eb646b2706829bc05aade20",
        "Ipod-22504f0bfd115cf82a897f1d11ab4c9a",
        "Key-2f6a0237e53ffbdb59f37b6e82c5e93",
        "Knife-23fd9817d509fe472bf266a8f0187ce5",
        "Notepad-d79fa1cf4e528e4561e1dc6d433ab6c3",
        "Notepad-1bc4576e48647c81766f4de1ddad87e2",
        "Notepad-3ec8e454858a6aed2513f3510eefb345",
        "PersonStanding-6ba5e209ef4227f672b92bd86ed3f51f",
        "PersonStanding-6fc6ce57e672f74aae03309dca32a7d7",
        "PersonStanding-10477652cbfcbc0eeda994bbf9145c8e",
        "PersonStanding-72c61eeb6dee392ccffb3c5aabae5616",
        "PersonStanding-76d2877e6aec0cdc594f1d040b5eff5",
        "PersonStanding-a5c68439179e0b6c122158f85e386e74",
        "PersonStanding-a55fcf38ba4d1dec6b1b03503631d1a5",
        "PersonStanding-c318cb8f0d3df8dba3e9c423232171a1",
        "PersonStanding-f741864eb54d47d466da45b121c8ad2d",
        "Piano-77e8ddc7d00f3cfa85d624cfcd9a37a7",
        "PillBottle-1c6ac745bd297a28f32e3e25afe6badb",
        "PillBottle-2566f8400d964be69c3cb90632bf17f3",
        "Pillow-13d5f50d3c6547f89d7f930a71a4cc5d",
        "PowerStrip-78ab9744a27cad1eb3ee04e2bcfd53c5",
        "Pizza-d983461d98e8e159b534d1a189b06b60",
        "Pizza-d3b82280912a9f165db1171a63154512",
        "Radio-d6d24f50c1b69bd24c40f34de8f3bdb8",
        "Rock-1e3d1accb45d201df75b791738ce5d1e",
        "Rock-6d8b19cc20da276f75b791738ce5d1e",
        "Rock-5fd4a9a127715c58420a976683f645dc",
        "Rock-9345ebed0fe96305f75b791738ce5d1e",
        "Rock-2836312bce4dac18863a60a16aac3669",
        "Rock-a52a11494cf48c5a1b7a67fbb0b5aaae",
        "Rock-d11020dc8b2a832df75b791738ce5d1e",
        "Ship-c74dbde6656efb4eb4248d66f92b2feb",
        "Ship-ec3e0694850f1a923b7cda02ebfc1f62",
        "Telephone-2d4eb91f83a8d200e738f481f8560d58",
        "Telephone-180f5e2a874bceb1e4ba5fe92f32019a",
        "Telephone-bde1f43e04e08f64b594bbf2c494ab81",
        "Telephone-a5a29c03bca0ff22908c261630fec33",
        "Telephone-b0c6e40fb6cb0c59c4ac0877f73a0051",
        "Telephone-ff3b6aa8a4fecb2d5e50ebd844292d6e",
        "ToiletPaper-871c95ddf6fcfff5a2aebf4c02eb5852",
        "ToiletPaper-61f025961f877f5dc605801df3f66ec",
        "ToyFigure-29a37eb86f4677163f6b1b708c713b4a",
        "ToyFigure-18aef96d387bd96860467dddaf5cfe3f",
        "ToyFigure-14e4cb9d8fd5c29f60467dddaf5cfe3f",
        "ToyFigure-215e4b59683247378a3e8bd3a4e48433",
        "ToyFigure-e497ad9ede68029880d47325999bb00b",
        "ToyFigure-fc24d641b5a4d2a6a156590b19c3013b",
        "USBStick-44d0285ce88f2fc75630f88a70f71a06",
        "USBStick-71f6598d5426fb34c33dcf45f2780ed8",
        "USBStick-b415da321e0f08949ddf84e17f86ad6",
        "USBStick-fc821511ed3090ac2846fa729d90e125",
        "USBStick-e63515b22af08c1ab64d1cad3171545",
        "Vase-7f730baf68bbf8e79849e26e0fc388ed",
        "Vase-a891dc805b93d01370a43c2d978e502e",
        "Vase-ac0bbb80cceac3b5a876db93df8645ad",
        "Vase-adfbf2024ccc898f86c0096e326e950a",
        "Vase-e0855a7115eda487186588b1d2fb9823",
        "VideoGameConsole-4ca7d595469b59831021cd55d164c0ed",
        "VideoGameConsole-25d68a5e38a75618a31e1e53bb3107fe",
        "VideoGameConsole-6f63bae285167b09a56334a324a87883",
        "VideoGameConsole-86465ae67399dfb230ef2d42e83077df",
        "VideoGameConsole-85624a7f2c95a594624115b94f0915a1",
        "VideoGameController-97744246339b87d2d843d1e177c373a4",
        "WallClock-44316e91d4926a73fe9c9da4b1c78d03",
        "WallClock-be5cdc167aea5b3043d745d6c7c0579e",
        "WallClock-d9d5f19afabb621898d5fc0473d00a1c",
        "WallClock-fd8c09a07cf9e4358c159f8d5e761b17",
        "Watch-c2ad15817e54bd01f7a6ba154b1bfb92",
    ]

    bad_categories = set(bad_categories)
    bad_instances = set([ins.split("-")[-1] for ins in bad_instances])

    cnt = 0
    cat_list = {}

    with open(meta_path, "r", newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            flag = False
            if row['category'] == "":
                continue
            if row['fullId'][4:] in bad_instances:
                continue
            for cat in row['category'].split(","):
                if cat[0] == "_":
                    continue
                if cat in bad_categories:
                    continue
                cnt += 1
                if cat not in cat_list.keys():
                    cat_list[cat] = []
                cat_list[cat].append(row['fullId'][4:])
                break

    for x, y in cat_list.items():
        for i in y:
            # print(os.path.join(dst_path, "sem-" + x + "-" + i+".obj"))
            shutil.copyfile(
                os.path.join(src_path, i+".obj"), os.path.join(dst_path, "sem-" + x + "-" + i+".obj"))

    print("Extracted from ShapeNetSem:", cnt)


def extract_mujoco(src_path, dst_path):
    os.makedirs(dst_path, exist_ok=True)
    bad_instances = [
        "30_CONSTRUCTION_SET",
        "ALPHABET_AZ_GRADIENT",
        "ALPHABET_AZ_GRADIENT_WQb1ufEycSj",
        "Aroma_Stainless_Steel_Milk_Frother_2_Cup",
        "ASSORTED_VEGETABLE_SET",
        "BAGEL_WITH_CHEESE",
        "BAKING_UTENSILS",
        "BALANCING_CACTUS",
        "BATHROOM_CLASSIC",
        "BATHROOM_FURNITURE_SET_1",
        "BEDROOM_CLASSIC",
        "Avengers_Gamma_Green_Smash_Fists",
        "BEDROOM_NEO",
        "BIRD_RATTLE",
        "BRAILLE_ALPHABET_AZ",
        "BUILD_A_ROBOT",
        "BUILD_A_ZOO",
        "CAR_CARRIER_TRAIN",
        "CARSII",
        "CASTLE_BLOCKS",
        "CITY_TAXI_POLICE_CAR",
        "Clorox_Premium_Choice_Gloves_SM_1_pair",
        "CONE_SORTING",
        "CONE_SORTING_kg5fbARBwts",
        "Cootie_Game",
        "DINING_ROOM_CLASSIC",
        "DINING_ROOM_CLASSIC_UJuxQ0hv5XU",
        "DINNING_ROOM_FURNITURE_SET_1",
        "DOLL_FAMILY",
        "DPC_Thinsulate_Isolate_Gloves_Brown",
        "FAIRY_TALE_BLOCKS",
        "FARM_ANIMAL",
        "Fisher_price_Classic_Toys_Buzzy_Bee",
        "FOOD_BEVERAGE_SET",
        "FISHING_GAME",
        "FRUIT_VEGGIE_DOMINO_GRADIENT",
        "FRUIT_VEGGIE_MEMO_GRADIENT",
        "GARDEN_SWING",
        "GEARS_PUZZLES_STANDARD_gcYxhNHhKlI",
        "GEOMETRIC_SORTING_BOARD_MNi4Rbuz9vj",
        "HAPPY_ENGINE",
        "InterDesign_Over_Door",
        "In_Green_Company_Surface_Saver_Ring_10_Terra_Cotta",
        "JUICER_SET",
        "JUNGLE_HEIGHT",
        "LEGO_5887_Dino_Defense_HQ",
        "Lalaloopsy_Peanut_Big_Top_Tricycle",
        "LEGO_Duplo_Build_and_Play_Box_4629",
        "LEGO_Creationary_Game",
        "LEGO_Duplo_Creative_Animals_10573",
        "Lego_Friends_Mia",
        "MEAT_SET",
        "Melissa_Doug_Pattern_Blocks_and_Boards",
        "MODERN_DOLL_FAMILY",
        "MONKEY_BOWLING",
        "MOSAIC",
        "MOVING_MOUSE_PW_6PCSSET",
        "My_First_Animal_Tower",
        "MY_MOOD_MEMO",
        "Nickelodeon_The_Spongebob_Movie_PopAPart_Spongebob",
        "NUTS_BOLTS",
        "Object",
        "PARENT_ROOM_FURNITURE_SET_1_DLKEy8H4mwK",
        "PETS_ACCESSORIES",
        "Poppin_File_Sorter_Blue",
        "Poppin_File_Sorter_Pink",
        "Poppin_File_Sorter_White",
        "Progressive_Rubber_Spatulas_3_count",
        "Razer_BlackWidow_Ultimate_2014_Mechanical_Gaming_Keyboard",
        "Razer_Blackwidow_Tournament_Edition_Keyboard",
        "Razer_Naga_MMO_Gaming_Mouse",
        "Razer_Kraken_Pro_headset_Full_size_Black",
        "Razer_Naga_MMO_Gaming_Mouse",
        "Razer_Taipan_Black_Ambidextrous_Gaming_Mouse",
        "Razer_Taipan_White_Ambidextrous_Gaming_Mouse",
        "Remington_TStudio_Hair_Dryer",
        "Rexy_Glove_Heavy_Duty_Gloves_Medium",
        "RESCUE_CREW",
        "Rexy_Glove_Heavy_Duty_Large",
        "RJ_Rabbit_Easter_Basket_Blue",
        "ROAD_CONSTRUCTION_SET",
        "Rubbermaid_Large_Drainer",
        "SHAPE_MATCHING",
        "SHAPE_SORTER",
        "Superman_Battle_of_Smallville",
        "TABLEWARE_SET",
        "TABLEWARE_SET_5CHkPjjxVpp",
        "TABLEWARE_SET_5ww1UFLuCJG",
        "TEA_SET",
        "Thomas_Friends_Wooden_Railway_Ascending_Track_Riser_Pack",
        "TOWER_TUMBLING",
        "TWIST_SHAPE",
        "TWISTED_PUZZLE_twb4AyFtu8Q",
        "VEGETABLE_GARDEN",
        "Vtech_Cruise_Learn_Car_25_Years",
        "WATER_LANDING_NET",
    ]

    bad_instances = set(bad_instances)

    cnt = 0

    for mesh in tqdm(os.listdir(src_path)):
        if not os.path.exists(os.path.join(src_path,
                                           mesh, "model.obj")):
            continue
        if mesh in bad_instances:
            continue
        cnt += 1
        # print(os.path.join(dst_path, "mujoco-" + mesh+".obj"))
        shutil.copyfile(os.path.join(src_path,
                        mesh, "model.obj"), os.path.join(dst_path, "mujoco-" + mesh+".obj"))

    print("Extracted from Mujoco Models:", cnt)


def extract_ddg(src_path, dst_path):
    os.makedirs(dst_path, exist_ok=True)

    cnt = 0
    for code in tqdm(os.listdir(src_path)):
        if code[-4:] == ".off":
            cnt += 1
            # print(os.path.join(dst_path, "ddg-" + code[:-24] + ".obj"))
            mesh = trimesh.load(os.path.join(src_path, code),
                                force="mesh", process=False)
            mesh.export(os.path.join(dst_path, "ddg-" + code[:-24] + ".obj"))

    print("Extracted from DDG:", cnt)
